<script setup lang="ts">
import { showSuccessToast, showToast } from 'vant';
import { API } from '../api';
import { ref } from 'vue'
import { useRouter } from 'vue-router';
import { useRoute } from 'vue-router';
import { delToken, setStorage, setToken } from '../utils/cache';
const router = useRouter()
const route = useRoute()
console.log(route)
delToken()
const onSubmit = async (values: any) => {
    console.log('submit', values);
    const { data } = await API.baseControllerLogin(values)
    console.log("🚀 ~ onSubmit ~ data:", data)
    data.data?.accessToken && setToken(data.data.accessToken)
    const { data: { data: info } } = await API.userControllerDetail()
    setStorage('userInfo', JSON.stringify(info))
    console.log("🚀 ~ onSubmit ~ info:", info)
    showSuccessToast({ message: '登录成功~' });
    let redirect = String(route.query.redirect || '')
    const has = router.getRoutes().filter(d => redirect.startsWith(d.path))[0]
    router.push(has ? redirect : '/')
};
const showLoin = ref(false)
const form = ref({ account: '', password: '' })
async function submitBus() {
    return showToast('暂不支持~');
    const { f, code, state } = route.query
    alert(String(f) + String(code) + String(state))
    f && localStorage.setItem('f', String(f))
    state && localStorage.setItem('f', String(state))
    if (!code) {
        const u = encodeURI('https://www.iskxz.com/xyc')
        const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7af0231b6586d0dc&redirect_uri=${u}&response_type=code&scope=snsapi_userinfo&state=${f}#wechat_redirect`
        location.href = url
    } else {
        const { data } = await API.baseControllerGetXycUserInfo({ code: String(code) })
        alert(JSON.stringify(data))
    }
}
</script>
<template>
    <div class="form_btn" v-if="showLoin">
        <van-button type="primary" block class="btn1" @click="showLoin = false">店员登录</van-button>
        <van-button type="success" block @click="submitBus">顾客登录</van-button>
    </div>
    <van-form @submit="onSubmit" class="form" v-if="!showLoin">
        <van-cell-group inset>
            <van-field v-model.trim="form.account" name="account" label="用户名" placeholder="用户名"
                :rules="[{ required: true, message: '请输入用户名' }]" />
            <van-field v-model.trim="form.password" type="password" name="password" label="密码" placeholder="用户密码"
                :rules="[{ required: true, message: '请填写用户密码' }]" />
        </van-cell-group>
        <div style="margin: 16px;">
            <van-button round block type="primary" native-type="submit">
                提交
            </van-button>
        </div>
        <div class="intro">
            <div class="intro_msg">
                请联系品牌人员进行报名并支付49.9元，即可领取邀请函锁定以下特权福利↓↓↓
            </div>
            <img class="img" src="../assets/img/login_intro.png">
        </div>
    </van-form>

</template>

<style scoped lang="less">
.form,
.form_btn {
    padding: 30px 0;
    min-height: 100vh;
    width: 100vw;
}

.form_btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 30px;

    .btn1 {
        margin-bottom: 20px;
    }
}
.intro{
    padding: 10px 20px;
    .intro_msg{
        color: red;
    }
    .img{
        width: 100%;
        padding: 10px 0 0;
    }
}
</style>
